package com.lcu.wl.modules.tools.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcu.wl.common.utils.FileUtil;
import com.lcu.wl.common.utils.PageUtils;
import com.lcu.wl.common.utils.Query;
import com.lcu.wl.common.utils.StringUtils;
import com.lcu.wl.modules.tools.entity.QuartzJob;
import com.lcu.wl.modules.tools.entity.QuartzLog;
import com.lcu.wl.modules.tools.mapper.QuartzLogMapper;
import com.lcu.wl.modules.tools.service.IQuartzLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时任务日志 服务实现类
 * </p>
 *
 * @author wanglei
 * @since 2021-03-18
 */
@Service("quartzLogService")
public class QuartzLogServiceImpl extends ServiceImpl<QuartzLogMapper, QuartzLog> implements IQuartzLogService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String beanName = (String) params.get("beanName");
        String createTime = (String) params.get("createTime");
        String endTime = (String) params.get("endTime");
        String status = (String) params.get("status");

        QueryWrapper<QuartzLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(status)){
            queryWrapper.eq("status",status);
        }

        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)){
            queryWrapper.between("create_time",createTime,endTime);
        }

        IPage<QuartzLog> page = this.page(new Query<QuartzLog>().getPage(params),
                queryWrapper.like(StringUtils.isNotBlank(beanName),"bean_name", beanName)
        );

        return new PageUtils(page);
    }

    @Override
    public List<QuartzLog> queryAll(Map<String, Object> params) {
        String beanName = (String) params.get("beanName");
        String createTime = (String) params.get("createTime");
        String endTime = (String) params.get("endTime");
        String status = (String) params.get("status");

        QueryWrapper<QuartzLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(status)){
            queryWrapper.eq("status",status);
        }

        if (StringUtils.isNotBlank(createTime) && StringUtils.isNotBlank(endTime)){
            queryWrapper.between("create_time",createTime,endTime);
        }

        List<QuartzLog> quartzLogList = this.list(queryWrapper.like(StringUtils.isNotBlank(beanName),"bean_name", beanName));

        return quartzLogList;
    }

    @Override
    public void download(List<QuartzLog> quartzLogLists, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (QuartzLog quartzLog : quartzLogLists) {
            Map<String,Object> map = new LinkedHashMap<>();

            map.put("任务ID", quartzLog.getJobId());
            map.put("Bean名称", quartzLog.getBeanName());
            map.put("参数", quartzLog.getParams());
            map.put("状态", quartzLog.getStatus() == 1 ? "失败" : "成功");
            map.put("耗时",quartzLog.getTimes());
            map.put("错误详情", quartzLog.getError());
            map.put("创建日期", quartzLog.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
